<?php

/**
 * Menashop
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this project in the file LICENSE.txt
 *
 * @category   Zend
 * @package    Storefront_Model
 * @subpackage Category
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

/**
 * The domain for category
 * 
 * @category   Zend
 * @package    Storefront_Model
 * @subpackage Category
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 */
class Storefront_Model_Category extends Zend_Db_Table_Abstract
{

    /**
     * The name of table
     * @var string 
     */
    protected $_name = 'category';

    /**
     * Find root category
     * @return Storefront_Model_Category
     */
    protected function _findRoot()
    {
        $query = $this->getAdapter()->select();
        $query->from($this->_name, array('id', 'has_children'));
        $query->where('id = parent_id');
        $result = $this->getAdapter()->fetchRow($query, null, Zend_Db::FETCH_OBJ);
        return $result;
    }

    /**
     * Get categories that have products
     * @param integer $languageId
     * @return array
     */
    protected function _getAllThatHaveProducts($languageId)
    {
        $query = $this->getAdapter()->select();
        $query->from(array('c' => $this->_name), array('c.id', 'c.parent_id', 'c.has_children', 'c.thumb_pic'));
        $query->join(array('ct' => 'category_translation'), 'c.id = ct.category_id', array('ct.name'));
        $query->where('c.has_products = 1');
        $query->where('ct.language_id = ?', $languageId);
        $result = $this->getAdapter()->fetchAll($query, null, Zend_Db::FETCH_OBJ);
        foreach ($result as $row) {
            $row->name = mb_strtoupper(mb_substr($row->name, 0, 1, 'utf8'), 'utf8') . mb_substr($row->name, 1, 255, 'utf8');
        }
        return $result;
    }

    /**
     * Find category by ID and language
     * @param integer $id 
     * @param integer $languageId 
     * @return Storefront_Model_Category
     */
    public function findByIdAndLanguage($id, $languageId)
    {
        $query = $this->getAdapter()->select();
        $query->from(array('c' => $this->_name), array('c.id', 'c.has_children', 'c.low_pic', 'c.thumb_pic'));
        $query->join(array('ct' => 'category_translation'), 'c.id = ct.category_id', array('ct.name'));
        $query->where('c.id = ?', $id);
        $query->where('ct.language_id = ?', $languageId);
        $result = $this->getAdapter()->fetchRow($query, null, Zend_Db::FETCH_OBJ);
        if ($result) {
            $result->name = mb_strtoupper(mb_substr($result->name, 0, 1, 'utf8'), 'utf8') . mb_substr($result->name, 1, 255, 'utf8');
        }
        return $result;
    }

    /**
     * Get children of category
     * @param integer $parentId 
     * @param integer $languageId
     * $return array
     */
    public function getChildren($parentId, $languageId)
    {
        $query = $this->getAdapter()->select();
        $query->from(array('c' => $this->_name), array('c.id', 'c.has_children', 'c.thumb_pic'));
        $query->join(array('ct' => 'category_translation'), 'c.id = ct.category_id', array('ct.name'));
        $query->where('c.has_products = 1');
        $query->where('c.parent_id = ?', $parentId);
        $query->where('ct.language_id = ?', $languageId);
        $result = $this->getAdapter()->fetchAll($query, null, Zend_Db::FETCH_OBJ);
        foreach ($result as $row) {
            $row->name = mb_strtoupper(mb_substr($row->name, 0, 1, 'utf8'), 'utf8') . mb_substr($row->name, 1, 255, 'utf8');
        }
        return $result;
    }

    /**
     * Get category tree
     * @param integer $languageId
     * @return Storefront_Model_Category 
     */
    public function getTree($languageId)
    {
        // get all categories with products from database
        $categories = $this->_getAllThatHaveProducts($languageId);
        // find root category in database
        $rootCategory = $this->_findRoot();
        if ($rootCategory) {
            // add root category to hash
            $rootCategory->children = array();
            $categoriesHash = array($rootCategory->id => $rootCategory);
            // add all categories to hash
            for ($i = 0; $i < count($categories); $i++) {
                $categories[$i]->children = array();
                $categoriesHash[$categories[$i]->id] = $categories[$i];
            }
            // update category hash
            for ($i = 0; $i < count($categories); $i++) {
                if ($categories[$i]->id != $rootCategory->id) {
                    //$categoryChildren = $categoriesHash[$categories[$i]->parent_id]->children;
                    array_push($categoriesHash[$categories[$i]->parent_id]->children, $categoriesHash[$categories[$i]->id]);
                }
            }
            return $categoriesHash[$rootCategory->id];
        }
    }

    /**
     * Find category IDs by name
     * @param string $name 
     * @return array
     */
    public function findIdsByName($name)
    {
        $query = $this->getAdapter()->select()->distinct();
        $query->from(array('c' => $this->_name), array('c.id'));
        $query->join(array('ct' => 'category_translation'), 'c.id = ct.category_id', array('ct.name'));
        $query->where('ct.name like ?', '%' . $name . '%');
        $result = $this->getAdapter()->fetchCol($query);
        return $result;
    }

    /**
     * Get parent categories
     * @param integer $categoryId
     *      Category ID
     * @param type $languageId
     *      Language ID
     * @return array 
     */
    public function getParentCategories($categoryId, $languageId)
    {
        $categories = array();

        // Get information about current category
        $query = $this->getAdapter()->select();
        $query->from(array('c' => $this->_name), array('c.id', 'c.parent_id'));
        $query->join(array('ct' => 'category_translation'), 'c.id = ct.category_id', 'ct.name');
        $query->where('c.id = ?', $categoryId);
        $query->where('ct.language_id = ?', $languageId);
        $row = $this->getAdapter()->fetchRow($query, null, Zend_Db::FETCH_OBJ);

        // Check result for current category
        if ($row) {
            $row->name = mb_strtoupper(mb_substr($row->name, 0, 1, 'utf8'), 'utf8') . mb_substr($row->name, 1, 255, 'utf8');
            array_push($categories, $row);

            // Get information about parent categories
            $parentId = $row->parent_id;
            if ($categoryId != $parentId) {
                $categories = array_merge($categories, $this->getParentCategories($parentId, $languageId));
            }
        }
        return $categories;
    }

}

